package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class OrderdetailetDaoImpl implements OrderdetailetDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    public int addOrderDetail(List<Orderdetailet> orderdetailets) throws Exception{
        int result=0;
        String sql="insert into Orderdetailet(orderId,foodId,quantity) values(?,?,?)";
        for (int i = 1; i < orderdetailets.size(); i++) {
            sql = sql.concat(",(?,?,?)");
        }
        try {
            //获取连接
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            int count = 1;
            for (Orderdetailet od : orderdetailets) {
                pst.setInt(count++, od.getOrderId());
                pst.setInt(count++, od.getFoodId());
                pst.setInt(count++, od.getQuantity());
            }
            result = pst.executeUpdate();
        }
        finally {
            DBUtil.close(rs,pst);
        }
        return result;
    }
}
